package com.zhiyu.controller;

import com.zhiyu.entity.DO.PermissionDO;
import com.zhiyu.entity.VO.ResponseVO;
import com.zhiyu.service.UserService;
import com.zhiyu.util.JWTUtil;
import com.zhiyu.util.MapBuilder;
import com.zhiyu.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private ResponseUtil responseUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private JWTUtil jwtUtil;

    @RequestMapping(value = "/permissions", method = RequestMethod.GET)
    @RequiresAuthentication
    public ResponseVO getPermissions() {
        int userId = getUserId();
        List<PermissionDO> permissions = userService.findPermissions(userId);

        Map map = MapBuilder.newInstance().put("permissions", permissions).build();
        ResponseVO responseVO = responseUtil.success();
        responseVO.setData(map);

        return responseVO;
    }

    @RequestMapping(value = "/user/{userId}", method = RequestMethod.GET)
    public ResponseVO getUserInformation(@PathVariable int userId) {
        if(SecurityUtils.getSubject().isAuthenticated()) {
            int myId = getUserId();
            return userService.getUserInformation(myId, userId);
        } else {
            return userService.getUserInformation(-1, userId);
        }
    }

    @RequestMapping(value = "/me/info", method = RequestMethod.POST)
    @RequiresAuthentication
    @RequiresPermissions(value = "sys:user:info:update")
    public ResponseVO updateUserInformation(@RequestParam String field, @RequestParam String value) {
        int userId = getUserId();
        return userService.updateUserInformation(userId, field, value);
    }

    @RequestMapping(value = "/user/{userId}/followers", method = RequestMethod.POST)
    @RequiresAuthentication
    public ResponseVO follow(@PathVariable int userId) {
        int myId = getUserId();
        return userService.follow(myId, userId);
    }

    @RequestMapping(value = "/user/{userId}/followers", method = RequestMethod.DELETE)
    @RequiresAuthentication
    public ResponseVO unfollow(@PathVariable int userId) {
        int myId = getUserId();
        return userService.unfollow(myId, userId);
    }

    @RequestMapping(value = "/user/{userId}/followers", method = RequestMethod.GET)
    public ResponseVO getFollower(@PathVariable int userId, @RequestParam int offset, @RequestParam int limit) {
        if(SecurityUtils.getSubject().isAuthenticated()) {
            int myId = getUserId();
            return userService.getFollowers(myId, userId, offset, limit);
        } else {
            return userService.getFollowers(-1, userId, offset, limit);
        }
    }

    @RequestMapping(value = "/user/{userId}/followings", method = RequestMethod.GET)
    public ResponseVO getFollowings(@PathVariable int userId, @RequestParam int offset, @RequestParam int limit) {
        if(SecurityUtils.getSubject().isAuthenticated()) {
            int myId = getUserId();
            return userService.getFollowings(myId, userId, offset, limit);
        } else {
            return userService.getFollowings(-1, userId, offset, limit);
        }
    }

    private int getUserId() {
        String jwtToken = (String)SecurityUtils.getSubject().getPrincipal();
        String subject = jwtUtil.getSubject(jwtToken);
        return Integer.valueOf(subject);
    }

}
